package com.library.filepicker.demo;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.xh.filepicker.sdk.XHFilePicker;

import java.util.List;

/**
 * Created by zhuangshaobo on 2021/6/22.
 */
public class DemoActivity extends AppCompatActivity {

    private CheckBox mSelectDirCb;
    private Button mSelectDirBtn;
    private CheckBox mSelectMultiCb;
    private EditText mSelectMaxEt;
    private CheckBox mFileFilterCb;
    private EditText mFileFilterEt;
    private CheckBox mShowHiddenCb;
    private CheckBox mShowDividerCb;
    private CheckBox mOnlyDirCb;
    private Button mStartBtn;

    private String rootDir;

    @Override
    protected void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        mSelectDirCb = findViewById(R.id.cb_select_dir);
        mSelectDirBtn = findViewById(R.id.btn_select_dir);
        mSelectMultiCb = findViewById(R.id.cb_select_multi);
        mSelectMaxEt = findViewById(R.id.et_select_max);
        mFileFilterCb = findViewById(R.id.cb_file_filter);
        mFileFilterEt = findViewById(R.id.et_file_filter);
        mShowHiddenCb = findViewById(R.id.cb_show_hidden);
        mShowDividerCb = findViewById(R.id.cb_show_divider);
        mOnlyDirCb = findViewById(R.id.cb_only_dir);
        mStartBtn = findViewById(R.id.btn_start);

        mSelectDirBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                XHFilePicker.from(DemoActivity.this)
                        .addConfigBuilder()
                        .showOnlyDirectory(true)
                        .showHiddenFiles(false)
                        .addItemDivider(true)
                        .build()
                        .forResult(100);
            }
        });

        mStartBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                XHFilePicker.from(DemoActivity.this)
                        .addConfigBuilder()
                        .setRootDirectory(mSelectDirCb.isChecked() ? rootDir : "")
                        .selectMultipleFiles(mSelectMultiCb.isChecked())
                        .selectMaxNum(!TextUtils.isEmpty(mSelectMaxEt.getText())? Integer.parseInt(mSelectMaxEt.getText().toString()) : 0)
                        .setFilters(mFileFilterCb.isChecked() && !TextUtils.isEmpty(mFileFilterEt.getText()) ? mFileFilterEt.getText().toString().split(";") : null)
                        .showHiddenFiles(mShowHiddenCb.isChecked())
                        .addItemDivider(mShowDividerCb.isChecked())
                        .showOnlyDirectory(mOnlyDirCb.isChecked())
                        .build()
                        .forResult(101);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {
            switch (requestCode) {
                case 100:
                    //单选取第一个,例如：files.get(0)
                    List<String> files = data.getStringArrayListExtra(XHFilePicker.RESULT_DATA);
                    rootDir = files.get(0);
                    mSelectDirCb.setText("根目录："+rootDir);
                    break;
                case 101:
                    List<String> pathList = data.getStringArrayListExtra(XHFilePicker.RESULT_DATA);
                    showListDialog(pathList);
                    break;
            }
        }
    }

    private void showListDialog(List<String> itemList) {
        String[] items = itemList.toArray(new String[itemList.size()]);
        AlertDialog listDialog = new AlertDialog.Builder(this)
                .setTitle("选中的文件路径")
                .setItems(items, null)
                .setPositiveButton("关闭", null)
                .create();
        listDialog.show();
    }
}
